#! /usr/bin/python2.4

# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""bypass.py unittest"""


import unittest

import svn.fs
import svn.repos

import gvn.hooks.info
import gvn.hooks.pre_commit.bypass

import testing.hooks.common

# We've stored the changes we need in revision 4 of the test repository
TESTREV = 4

SUPER_GROUP = "super-group"


class TestCase(testing.hooks.common.HookTestCase):

  def setUp(self):
    # HookTestCase creates and sets a _logger object based on whether
    # GVN_TEST_DEBUG is exported or not
    testing.hooks.common.HookTestCase.setUp(self)

    # create group pickle to use as our group file
    user_groups= {
      "polly": [ "group1" ],
      "adminuser": [ SUPER_GROUP ],
    }

    for user in user_groups:
      self.userdb.WriteUserGroups(user, user_groups[user])


  def _TestProps(self, prop_name, prop_val, author, path=""):
    """Return RunHook result triple from txn based on arguments.

    Args:
      prop_name: which property to create
      prop_val: which property value to set
      author: who owns that transaction
      path: where to graft the property or "REV" for revprop

    Returns:
      0 (pass), 1 (fail), string (fail and display string)

    Make a txn by setting the property to / and check if author can submit it
    """
    # seed argv with expected arguments during a regular call, and overwrite
    # the ones we need to, with our own values lower down
    argv = ["pre-commit", "repo_name_bogus_ignored", "txnname_bogus_ignored"]
    hi = gvn.hooks.info.HookInfo(argv, userdb=self.userdb)

    # plug in the actual repo and txn objects, from which hi can compute fs
    hi._repo = svn.repos.open(self.repo_name, hi.pool)
    hi._txn = testing.hooks.common.BuildPropTxn(hi.fs, TESTREV,
                       prop_name, prop_val, path, author, "boguslog", hi.pool)

    hi._super_users = (set(), set([SUPER_GROUP]))

    ret = gvn.hooks.pre_commit.bypass.RunHook(hi, self._logger)

    # Delete/clean up transaction
    svn.fs.abort_txn(hi.txn, hi.pool)
    return ret


  def testSuperUserGvnSuperUsersProp(self):
    """Check setting superusers property from superuser.
    """
    self.assertHookResult(self._TestProps("gvn:superusers", "group: svn-team",
                          "adminuser"), 0)

  def testRandomUserGvnSuperUsersProp(self):
    """Check setting superusers property from a regular user.
    """
    self.assertHookResult(self._TestProps("gvn:superusers", "group: fowls",
                         "polly"), 0)

  def testSuperUserGvnBypassHookProp(self):
    """Check setting gvn:bypass-hooks property from a superuser.
    """
    self.assertHookResult(self._TestProps("gvn:bypass-hooks", "1",
                          "adminuser", "REV"), -1)

  def testRandomUserGvnBypassHookProp(self):
    """Check setting gvn:bypass-hooks property from a regular user.
    """
    self.assertHookResult(self._TestProps("gvn:bypass-hooks", "1",
                         "polly", "REV"),
                         "Non Superuser polly tried to invoke gvn:bypass-hooks")


if __name__ == "__main__":
  unittest.main()
